home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
graphic
/
fgdemo10.zip
/
EDITOR.C
< prev
next >
Wrap
Text File
|
1991-10-05
|
29KB
|
1,009 lines
/**********************************************************************\
* *
* editor.c -- this is a primitive graphics mode editor with word-wrap *
* It can be used as a template for a graphics mode editor, but it is *
* not certified bug-free, and anybody thinking of using this code *
* should study it carefully, beta-test it thoroughly, and debug as *
* appropriate. This code is presented here primarily for academic *
* purposes: it is one approach to the problem of how to write a *
* graphics mode text editor. *
* *
* Use this code freely, and send suggestions for optimizations and *
* improvements to Diana Gruber. *
* *
\**********************************************************************/
#include "defs.h"
#define CTRL_Y 25
#define F1 59
#define F2 60
#define F9 67
#define F10 68
#define CHAR_WIDTH 8
int overstrike;
int top_row, bottom_row;
/**********************************************************************\
* *
* can_wrap -- is there enough room to do a word wrap? *
* *
\**********************************************************************/
can_wrap(s1,s2,length)
char *s1, *s2;
int length;
{
if (strlen(s1)+strlen(s2) < length)
return(TRUE);
else
return(FALSE);
}
/**********************************************************************\
* *
* clear_string -- clear all or part of a row of chars *
* *
\**********************************************************************/
void clear_string(x,y,len,maxx)
int x, y, len, maxx;
{
int x1, y0;
int color;
if (len == 0) return;
y0 = y - ptsize;
x1 = x + len * CHAR_WIDTH - 1;
if (x1 > maxx) x1 = maxx;
color = fg_getcolor();
fg_setcolor(background);
fg_rect(x,x1,y0,y);
fg_setcolor(color);
}
/**********************************************************************\
* *
* editor -- edit nrows lines of text *
* *
\**********************************************************************/
editor(array,minx,maxx,miny,maxy,nrows,array_len)
char *array;
int minx, miny, maxx, maxy, nrows;
int *array_len;
{
register int i, n;
int b, l;
int x, y;
int iy;
unsigned char key, aux;
static char char_string[] = {0,0};
int maxcol;
int row;
char *tempstring;
char *string[101];
int col[101];
int end[101];
int color;
int visible_rows;
color = fg_getcolor();
fg_setpage(visual);
overstrike = FALSE;
iy = miny + ptsize;
/* determine the string size */
maxcol = (maxx+1 - minx) / CHAR_WIDTH;
/* allocate space for the strings and initialize all elements to 0 */
for (n = 0; n < nrows; n++)
{
if ((string[n] = calloc(maxcol+3,sizeof(char))) == NULL)
return(ERR);
col[n] = 0;
end[n] = 0;
}
tempstring = calloc(maxcol+2,sizeof(char));
/* display whatever is in the array now */
l = 0;
x = minx;
y = iy;
top_row = 0;
bottom_row = ((maxy - miny + 1) / (ptsize+1));
if (bottom_row > nrows) bottom_row = nrows;
/* recalc maxy based on ptsize, for scrolling */
maxy = iy + 1 + (bottom_row-1) * (ptsize + 1);
/* recalc nrows because they don't seem to come out right? */
nrows++;
for (row = top_row; row < bottom_row; row++)
{
put_string(&array[l],x,y);
strcpy(string[row],&array[l]); /* copy it to the temp string */
unblank(string[row]);
l += strlen(&array[l]) + 1;
col[row] = strlen(string[row]);
y += ptsize + 1;
}
/* if there any rows beyond the bottom row */
for (row = bottom_row; row<nrows; row++)
{
strcpy(string[row],&array[l]);
l += strlen(&array[l]) + 1;
unblank(string[row]);
col[row] = strlen(string[row]);
}
flushkey();
row = 0; /* starting position */
col[row] = 0;
x = get_x(minx,col[row]);
y = iy;
put_editor_cursor(x,y);
visible_rows = bottom_row - top_row;
while (TRUE)
{
fg_getkey(&key,&aux);
/* return */
if (key == CR)
{
unblank(string[row]);
remove_editor_cursor(x,y);
if (row < bottom_row - 1)
{
n = end_paragraph(row,string);
if (n < bottom_row - 1)
{
for (i = n; i > row; i--)
{
strcpy(string[i+1],string[i]);
y = get_y(iy,i+1);
clear_string(minx,y,maxcol,maxx);
put_string(string[i+1],minx,y);
}
strcpy(string[row+1],&string[row][col[row]]);
string[row][col[row]] = NULL;
x = get_x(minx,col[row]);
y = get_y(iy,row);
clear_string(x,y,maxcol,maxx);
y = get_y(iy,row+1);
clear_string(minx,y,maxcol,maxx);
put_string(string[row+1],minx,y);
}
row++;
}
else if (bottom_row < nrows - 1)
{
fg_setcolor(background);
fg_scroll(minx,maxx,(iy+1),maxy,-(ptsize+1),1);
fg_setcolor(color);
bottom_row++;
top_row++;
row++;
y = get_y(iy,bottom_row-1);
put_string(string[bottom_row-1],minx,y);
}
else
row = top_row;
col[row] = 0;
x = get_x(minx,col[row]);
y = get_y(iy,row);
put_editor_cursor(x,y);
}
/* F1 -- help screen */
else if (aux == F1)
{
remove_editor_cursor(x,y);
help_screen();
fg_setcolor(15);
put_editor_cursor(x,y);
}
/* back space */
else if (key == BS && col[row] > 0)
{
remove_editor_cursor(x,y);
col[row]--;
string[row][col[row]] = NULL; /* remove a character */
strcpy(tempstring,string[row]);
strcat(tempstring,&string[row][col[row]+1]);
strcpy(string[row],tempstring);
x = get_x(minx,col[row]);
clear_string(x,y,maxcol,maxx);
put_string(&string[row][col[row]],x,y);
put_editor_cursor(x,y);
}
/* delete */
else if (aux == DELETE)
{
if (string[row][col[row]] == NULL && row < bottom_row-1)
/* delete at end of line */
{
end[row] = strlen(string[row]);
end[row+1] = strlen(string[row+1]);
b = first_blank(string[row+1]);
if (end[row] + b <= maxcol)
{
remove_editor_cursor(x,y);
/* see if you can wrap more than one word */
while (end[row]+b <= maxcol && b < end[row+1])
{
n = b;
b += first_blank(&string[row+1][b+1]) + 1;
}
if (end[row]+b > maxcol) b = n;
if (string[row+1][b] == NULL) /* wrap whole line */
{
strcat(string[row],string[row+1]); /* move all lines up */
for (i = row+1; i < bottom_row; i++)
{
strcpy(string[i],string[i+1]);
y = get_y(iy,i);